package com.lsc.admin.demo.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
/***
 * 线程池配置
 * @author ln 2021-11-30
 *
 */
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
 
  @Override
  public Executor getAsyncExecutor() {
      ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
      executor.setCorePoolSize(16);//核心线程数目
      executor.setMaxPoolSize(16);//最大线程数目Integer.MAX_VALUE
      executor.setQueueCapacity(100);//队列中最大的数目
      executor.setKeepAliveSeconds(60); //线程空闲时间
      executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //超出队列处理方式
      executor.initialize();
      return executor;
  }
 
}